package com.eig.courseweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eig.common.entity.course.Video;
import com.eig.common.vo.Result;
import com.eig.courseweb.dao.VideoDao;
import com.eig.courseweb.service.VideoService;
import com.eig.courseweb.utils.IdWorker;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2023/01/12 下午 09:59
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoDao videoDao;
    @Resource
    private IdWorker idWorker;

    @Override
    public Result insertVideo(Video video) {

        video.setId(idWorker.nextId());
        video.setSize(0);
        video.setStatus(1);
        video.setVersion(1);
        video.setPlayCount(0);
        videoDao.insertVideo(video);
        return new Result(2000, "操作成功");
    }

    @Override
    public Result findAllVideo(String chapterId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chapter_id", chapterId);
        List<Video> list = videoDao.selectList(wrapper);
        return new Result(2000, "操作成功", list);
    }

    @Override
    public Result deleteById(String id, int sort, String chapterId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id", id);
        wrapper.eq("sort", sort);
        wrapper.eq("chapter_id", chapterId);
        videoDao.delete(wrapper);
        return new Result(2000, "操作成功");
    }

    @Override
    public Result updateVideo(Video video) {
        UpdateWrapper update = new UpdateWrapper();
        if (ObjectUtil.isNotEmpty(video.getTitle())){
            update.set("title",video.getTitle());
        }
        if (ObjectUtil.isNotEmpty(video.getVideoSourceId())){
            update.set("video_source_id",video.getVideoSourceId());
        }
        if (ObjectUtil.isNotEmpty(video.isFree())){
            update.set("is_free",video.isFree());
        }
        if (ObjectUtil.isNotEmpty(video.getVideoOriginalName())){
            update.set("video_original_name",video.getVideoOriginalName());
        }
        update.like("id",video.getId());
        videoDao.update(video, update);
        return new Result(2000, "操作成功");
    }

    @Override
    public Result queryAllVideoByCourseId(String id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id", id);
        List list = videoDao.selectList(wrapper);
        return new Result(2000, "操作成功",list);

    }
}
